function deviceType() {
    const ua = navigator.userAgent;
    const isIos = ua.indexOf('iPad') > 0 || (ua.indexOf('iPhone') > 0 || ua.indexOf('iPod') > 0);
    const isAndroid = ua.indexOf('Android') > 0;
    const isWinPhone = ua.indexOf('Windows Phone') > 0;
    const isWindows = ua.indexOf('Windows NT') > 0;
    if (isIos) {
        return 'ios';
    } else if (isAndroid) {
        return 'android';
    } else if (isWinPhone) {
        return 'wp';
    } else if (isWindows) {
        return 'windows';
    } else {
        return 'other';
    }
}
function unSupport(msg = '不支持此设备') {
    if (!document.getElementById('_unSupport')) {
        let win = document.createElement('div');
        win.id = '_unSupport';
        win.innerHTML = msg;
        win.style.textAlign = 'center';
        win.style.color = '#999';
        document.body.appendChild(win);
    }
}

function connectWebViewJavascriptBridge(callback) {
    if (typeof callback !== 'function') {
        throw new Error('callback参数必须是个function！！！');
    }
    if (window.WebViewJavascriptBridge) {
        callback(window.WebViewJavascriptBridge);
    } else {
        document.addEventListener('WebViewJavascriptBridgeReady', () => {
            callback(window.WebViewJavascriptBridge);
        }, false);
    }
}

function call({ methodFromApp, args, callBack }) {
    const d = deviceType();
    switch (d) {
    case 'ios':
        try {
            connectWebViewJavascriptBridge((bridge) => {
                bridge.callHandler(methodFromApp, args, callBack);
            });
        } catch (e) {
            return false;
        }
        break;
    case 'android':
        try {
            if (!!args && !callBack) {
                window.AndroidWebView[methodFromApp](JSON.stringify(args));
            } else if (!args && !callBack) {
                window.AndroidWebView[methodFromApp]();
            } else if (!args && !!callBack) {
                register(methodFromApp, callBack);
                window.AndroidWebView[methodFromApp](JSON.stringify(args));
            } else if (!!args && !!callBack) {
                register(methodFromApp, callBack);
                window.AndroidWebView[methodFromApp]();
            }
        } catch (e) {
            return false;
        }
        break;
    case 'wp':
        unSupport('暂不支持windows phone');
        break;
    case 'windows':
        unSupport('请使用手机访问');
        break;
    default:
        unSupport();
        break;
    }
}

function register(methodName, methodBLock) {
    const d = deviceType();
    switch (d) {
    case 'ios':
        try {
            connectWebViewJavascriptBridge((bridge) => {
                bridge.registerHandler(methodName, methodBLock);
            });
        } catch (e) {
            return false;
        }
        break;
    case 'android':
        if (!window.regMethod) {
            window.regMethod = {};
        }
        window.regMethod[methodName] = methodBLock;
        break;
    case 'wp':
        unSupport('暂不支持windows phone');
        break;
    case 'windows':
        unSupport('请使用手机访问');
        break;
    default:
        unSupport();
        break;
    }
}

export { call, register, deviceType };
